// //1. 导入 express
// const express = require('express');
// const app = express();
// app.use(express.json());
// //2. 创建应用对象

// const cors = require('cors');
// app.use(cors());
// const yjstudentRouter= require('./routes/yjstudentRouter')
// const loginRouter= require('./routes/loginRouter')
// app.use('/api/yjstudent',yjstudentRouter)
// app.use('/api/login',loginRouter)

// //1.导入用于生成jwt的字符串的包
// const jwt = require('jsonwebtoken');
// //2.导入用于将客户端发来的jwt字符串解析还原成JSON对象的包
// const expressJwt = require('express-jwt');
// //3.定义 secret 密钥（应该保密）,它会在后续生成jwt字符串时被使用
// const key="jwtSecret123456"

// app.use(expressJwt({secret:key,algorithm:['HS256']}).unless({path:['/test','/api/login']}))




// app.listen(8000, () => {
//     console.log('Server is running on port 8000');
//   });
  // 导入 express
  const express = require('express');
  const app = express();
  app.use(express.json());
  const path = require('path');
  const fs = require('fs');
  app.use(express.static('public'))
  // 设置静态目录
  // app.use('./public/loads',express.static(path.join(__dirname, './public/loads')))
  app.use('/public', express.static('public'));
  const cors = require('cors');
  const jwt = require('jsonwebtoken');
  const { expressjwt: expressJwt } = require('express-jwt');
  const yjstudentRouter = require('./routes/yjstudentRouter');
  const loginRouter = require('./routes/loginRouter');
  const userRouter = require('./routes/userRouter');
  const blogRouter = require('./routes/bolgRouter');
  const commentRouter = require('./routes/commentRouter');
  const attachmentRouter = require('./routes/attachmentRouter');  
  const userRoutersss = require('./routes/userRoutersss'); 
  const jwtBlacklistRouter= require('./routes/jwtBlacklistRouter');
  const jwtBlacklistControll = require('./controllers/jwtBlacklistControll');
  app.use(cors());
  app.use('/api/yjstudent', yjstudentRouter);
  app.use('/api/login', loginRouter);
  app.use('/api/user', userRouter);
  app.use('/api/usersss', userRoutersss);
  app.use('/api/blog', blogRouter);
  app.use('/api/comment', commentRouter);
  app.use('/api/attachment', attachmentRouter);
  app.use('/api/jwtBlacklist', jwtBlacklistRouter);
  const key = "Yangjunjwt123456";


// // JWT 黑名单检查中间件

// const blacklistCheck = async (req, payload, done) => {
//   const authHeader = req.headers.authorization;
//   if (!authHeader || !authHeader.startsWith('Bearer ')) {
//     return done(null, true); // 如果没有或格式不正确，视为黑名单
//   }
//   const token = authHeader.split(' ')[1];
//   const isBlacklisted = await jwtBlacklistControll.isTokenBlacklisted(token);
//   if (isBlacklisted) {
//     return done(null, true); // Token 在黑名单中
//   }
//   return done(null, false); // Token 不在黑名单中
// };
// // 配置 JWT 验证中间件
// app.use(expressJwt({ secret: key, algorithms: ['HS256'], isRevoked: blacklistCheck }).unless({ path: ['/api/login', '/api/user/register'] }));

// // 错误处理中间件，用于处理 JWT 错误
// app.use((err, req, res, next) => {
//   if (err.name === 'UnauthorizedError') {
//     const token = req.headers.authorization.split(' ')[1];
//     jwtBlacklistControll.addTokenToBlacklist(token); // 将失效的 token 加入黑名单
//     res.status(401).json({ message: '未携带token或token失效' });
//   } else {
//     next(err);
//   }
// });

  
  app.listen(8000, () => {
      console.log('Server is running on port 8000');
  });
  